package com.study.七大原则.o3依赖倒置原则;

public class DependencyPass {

    public static void main(String[] args) {
//        new OpenAndClose().open(new TV1());
//        new OpenAndClose().open(new TV2());

//        new OpenAndClose(new TV1()).open();
//        new OpenAndClose(new TV2()).open();



        OpenAndClose openAndClose = new OpenAndClose();
        openAndClose.setTv(new TV1());
        openAndClose.open();

        OpenAndClose openAndClose2 = new OpenAndClose();
        openAndClose2.setTv(new TV2());
        openAndClose2.open();
    }
}

//方式一：通过接口传递，实现依赖
//interface IOpenAndClose{
//     void open(ITV tv);  //抽象方法  接收接口
//}
//
//interface ITV{
//     void play();
//}
////实现接口
//class OpenAndClose implements IOpenAndClose{
//    @Override
//    public void open(ITV tv) {
//        tv.play();
//    }
//}
//实现类  实现细节，通过接口传递
class TV1 implements ITV{
    @Override
    public void play() {
        System.out.println("高清大屏播放");
    }
}
class TV2 implements ITV{
    @Override
    public void play() {
        System.out.println("渣渣电视机播放");
    }
}


//方式二：通过构造方法依赖传递
//interface IOpenAndClose{
//    void open();   //抽象方法
//}
//interface ITV{
//    void play();
//}
//class OpenAndClose implements IOpenAndClose{
//    private ITV itv;  //声明接口为成员变量
//
//    public OpenAndClose(ITV itv) {   //通过构造器来传递依赖
//        this.itv = itv;
//    }
//
//    @Override
//    public void open() {
//        this.itv.play();
//    }
//}

//方式三：通过setter方法传递‘
interface IOpenAndClose{
    void open();
    void setTv(ITV tv);
}

interface ITV{
    void play();
}
class OpenAndClose implements IOpenAndClose{
    private ITV tv;  //声明接口为成员变量

    @Override
    public void open() {
        this.tv.play();
    }

    @Override
    public void setTv(ITV tv) {
        this.tv = tv;
    }

}





























